<template>
  <div>
    <el-row type="flex" justify='space-between' class="rows">
      <el-col :span="18">
        <el-form ref="form" :model="form" label-width="80px" inline>
          <el-form-item label="关键字">
            <el-input v-model="form.keyword"></el-input>
          </el-form-item>
          <el-form-item label="状态">
            <el-select v-model="form.state" placeholder="请选择">
              <el-option label="开启" value="1"></el-option>
              <el-option label="屏蔽" value="0"></el-option>
            </el-select>
          </el-form-item>
           <el-button @click="delList">清除</el-button>
        <el-button type="success" @click="checkList">搜索</el-button>
       </el-form>
      </el-col>
      <el-col :span="4"><el-button type="success" @click="addList">新增技巧</el-button></el-col>
    </el-row>
    <el-alert :title="page.total" type="info" :closable='false' show-icon class="alerts"></el-alert>
    <el-table :data="tableData" style="width: 100%" class="table">
      <el-table-column prop="date" label="序号" type="index" width="100px"></el-table-column>
      <el-table-column prop="title" label="文章列表" width="350px"></el-table-column>
      <el-table-column prop="visits" label="阅读数" width="300px"></el-table-column>
      <el-table-column prop="username" label="录入人" width="300px"></el-table-column>
      <el-table-column prop="createTime" label="录入时间" width="300px">
        <template slot-scope="{ row }">
          {{ parseTime(new Date(row.createTime)) || row.createTime }}
        </template>
      </el-table-column>
      <el-table-column prop="state" label="状态" width="200px">
        <template slot-scope="{ row }">
        {{row.state === 1 ? '开启' : '屏蔽'}}
        </template>
      </el-table-column>
        <el-table-column prop="address" label="操作">
        <template slot-scope="{ row }">
          <el-row>
            <el-col :span="6"><el-button type="text" @click="showDialog(row)">预览</el-button></el-col>
            <el-col :span="6"><el-button type="text" @click="forbidden">禁用</el-button></el-col>
            <el-col :span="6"><el-button type="text" @click="alterData(row)">修改</el-button></el-col>
            <el-col :span="6"><el-button type="text" @click="delData(row.id)">删除</el-button></el-col>
          </el-row>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      text-align='center'
      :page-sizes="[10, 20, 30]"
      :page-size="page.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="totals">
    </el-pagination>
    <el-dialog title="预览文章" :visible.sync="dialogVisible" width="36%">
      <el-row>
        <h1>{{rows.title}}</h1>
      </el-row>
      <el-row type='flex' justify='space-around'>
        <el-col><el-button type="text" style="color: 'black'">{{rows.title}}</el-button></el-col>
        <el-col><el-button type="text" style="color: 'black'">{{rows.username}}</el-button></el-col>
        <el-col><el-button icon="el-icon-view" type="text" style="color: 'black'">{{rows.visits}}</el-button></el-col>
      </el-row>
      <el-row>
        <div v-html="rows.articleBody"></div>
      </el-row>
    </el-dialog>
    <el-dialog title="提示" :visible.sync="showData" width="42%">
      <el-form :model="ruleForm" :rules="rules" ref="ruleFormRef" label-width="100px" class="demo-ruleForm">
        <el-form-item label="文章标题" prop="title">
          <el-input v-model="ruleForm.title"></el-input>
        </el-form-item>
        <el-form-item label="文章内容" prop="articleBody">
          <el-input type="textarea" :rows="2" placeholder="请输入内容" v-html="ruleForm.articleBody"/>
        </el-form-item>
        <el-form-item label="视频地址">
          <el-input v-model="ruleForm.videoURL"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="btncol">取 消</el-button>
        <el-button type="primary" @click="putData">确 定</el-button>
      </span>
    </el-dialog>
    <data-list :showDatas.sync='showDatas'></data-list>
  </div>
</template>

<script>
import { getList, putData, delData } from '@/api/user'
import { parseTime } from '@/utils'
import DataList from './component/DataList.vue'
export default {
  components: { DataList },
  data() {
    return {
      parseTime,
      form: {
        page: 1,
        pagesize: 10,
        keyword: '',
        state: ''
      },
      tableData: [],
      tableobj: {},
      totals: 0,
      page: {
        page: 1,
        pagesize: 10,
        pages: 10,
        total: ''
      },
      dialogVisible: false,
      rows: {},
      showData: false,
      ruleForm: {
        id: '',
        title: '',
        articleBody: '',
        videoURL: ''
      },
      rules: {
        title: [{ required: true, message: '请输入活动名称', trigger: 'blur' }],
        articleBody: [{ required: true, message: '请输入活动名称', trigger: 'blur' }]
      },
      showDatas: false
    }
  },
  created() {
    this.getList()
  },
  methods: {
    async getList() {
      const { data } = await getList(this.page)
      this.tableobj = data
      // console.log(this.tableobj)
      this.page.page = this.tableobj.page
      this.page.pages = this.tableobj.pages
      this.page.total = `共有${'' + this.tableobj.counts}条数据`
      this.totals = this.tableobj.counts
      this.tableData = data.items
    },
    handleSizeChange(newSize) {
      this.page.pagesize = newSize
      this.getList()
    },
    handleCurrentChange(newPage) {
      this.page.page = newPage
      this.getList()
    },
    showDialog(row) {
      this.dialogVisible = true
      this.rows = row
      console.log(this.rows)
    },
    alterData(row) {
      this.showData = true
      this.ruleForm.title = row.title
      this.ruleForm.articleBody = row.articleBody
      this.ruleForm.videoURL = row.videoURL
      this.ruleForm.id = row.id
    },
    async putData() {
      try {
        this.$refs.ruleFormRef.validate()
        const data = await putData(this.ruleForm)
        console.log(data)
        this.$message.success('修改成功')
      } catch (error) {
        console.log(error)
      }
      this.getList()
      this.showData = false
    },
    btncol() {
      this.ruleForm = {}
      this.$refs.ruleFormRef.resetFields()
      this.showData = false
    },
    async delData(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delData(id)
        this.getList()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    addList() {
      this.showDatas = true
    },
    async checkList() {
      const { data } = await getList(this.form)
      this.tableData = data.items
    },
    delList() {
      this.form = {
        page: 1,
        pagesize: 10,
        keyword: '',
        state: ''
      },
      this.getList()
    }
  }
}
</script>
<style lang="scss" scoped>
.alerts{
  margin-bottom: 20px;
}
.rows {
  margin-top: 20px;
}
</style>

